package com.dell.optimize;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.PowerManager;

public class DelayService extends Service{

	private Handler timer = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
		    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
		    wl.acquire();
			doServiceWork();
		}
	};

	private static long UPDATE_INTERVAL = 5 * 60 * 1000; // default

	private static long DELAY_INTERVAL = 0;
	
	private int RESTART_SERVICE = 1;
	private int DO_NOTHING = 2;

	Optimize rec;

	public IBinder onBind(Intent intent) {
		return null;
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		Util.setContext(this);
		Util.logMe("Service Started");
		doServiceWork();
		return START_STICKY;
	}

	/*@Override
	public void onCreate() {
		super.onCreate();

		//_startService();
		doServiceWork();
		SharedPreferences store = getSharedPreferences(Util.SETTINGS, 0);
		String msg = store.getString(Util.ERR_MSG, "");
		
		SharedPreferences.Editor editor = store.edit();
		Date cal = new Date();
		editor.putString(Util.ERR_MSG,msg+"\nService started at "+cal.toString());
		
		editor.commit();

	}
*/
	@Override
	public void onDestroy() {
		super.onDestroy();
//		_shutdownService();
		//stopService(new Intent(this, OptimizationService.class));
		
		try {
			rec.stop();
		} catch (IOException e) { // TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(" Exception while Destroying :" + e);
			Util.logMe("Error while stopping rec");
		}finally{
			rec = null;
		}
		 
		Util.logMe("Recorder Stopped");

	}

	

	/*
	 * start the processing, the actual work, getting config params, get data
	 * from network etc
	 */
	private void doServiceWork() {
		
		  if (rec != null) {
			try {
				rec.stop();
			} catch (IOException e) { // TODO Auto-generated catch block
				e.printStackTrace();
				Util.logMe("Unable to stop"+e);
			}
		}
		rec = new Optimize(this, "optimizerLog");
		try {

			rec.start();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println(" Exception while Creating : "  +e);
			Util.logMe("Exception while starting rec."+e);
		}
								 
		//stopService(new Intent(this, OptimizationService.class));
		// garbage collection
		// System.gc();
		//startService(new Intent(this, OptimizationService.class));
		
		timer.sendEmptyMessageDelayed(RESTART_SERVICE,UPDATE_INTERVAL);

	}

}
